package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.parm.BusinessLoginParm;
import com.ruoyi.system.domain.parm.SysMemberParm;
import com.ruoyi.system.service.ISysMemberFinanceService;
import com.ruoyi.system.service.ISysMemberService;
import com.ruoyi.system.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2024-03-31
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/mem/member")
public class MemMemberController extends BaseController
{
    @Autowired
    private ISysMemberService sysMemberService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISysMemberFinanceService sysMemberFinanceService;
    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String id) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }

        return success(sysMemberService.selectSysMemberById(id));
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    @Transactional
    public AjaxResult register(@RequestBody @Valid SysMemberParm sysMemberParm) throws Exception {

        SysMember sysMember = new SysMember();
        BeanUtils.copyProperties(sysMemberParm, sysMember);

        String memberId=UUID.randomUUID().toString();
        sysMember.setId(memberId);

        sysMemberService.insertSysMember(sysMember);

        // 添加 商家财务信息
        SysMemberFinance sysMemberFinance=new SysMemberFinance();
        sysMemberFinance.setMemberId( memberId );
        sysMemberFinance.setId(UUID.randomUUID().toString() );
        sysMemberFinanceService.insertSysMemberFinance(sysMemberFinance);


        return toAjax(1);
    }

    @ApiOperation(value = "获取用户登录信息")
    @GetMapping(value = "/getUserInfo")
    public AjaxResult getUserInfo()
    {
        return success(sysMemberService.getUserInfo());
    }

    /**
     * 商家登录【请填写功能名称】
     */
    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login")
    public AjaxResult Login(@RequestBody @Valid BusinessLoginParm businessLoginParm) throws Exception {

        SysMember re = sysMemberService.Login(businessLoginParm);

        if(re.getStatus()==0){
            throw new Exception("用户未激活");
        }
        if(re.getStatus()==2){
            throw new Exception("用户不可用");
        }

        //    String str = StirngUtil.generateRandomString(64);

        String json = JSONArray.toJSONString(re);
        //   redisTemplate.opsForValue().set(str,json, 86400, TimeUnit.SECONDS);

        String token = TokenUtil.memberCreate(String.valueOf(re.getId()), re.getUserName(), 36000);
        redisTemplate.opsForValue().set("member_"+re.getUserName(),json, 36000, TimeUnit.SECONDS);

        //   redisTemplate.expire(str, );
        Map result=new HashMap();
        result.put("member",re);
        result.put("Authorization",token);

        return success(result);
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "修改用户信息")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid SysMemberParm sysMemberParm) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }

        SysMember sysMember = new SysMember();
        BeanUtils.copyProperties(sysMemberParm, sysMember);
        sysMember.setUserName("");
        return toAjax(sysMemberService.updateSysMember(sysMember));
    }

}
